#include <bits/stdc++.h>
#define int long long
#define mid ((l+r)>>1)
using namespace std;
int a[200005],lsh[200005];
int dp1[200005],dp2[200005],add,max1,max2;
void solve(){
	for(int i=0;i<=200000;i++) dp1[i]=dp2[i]=-1e18; dp1[0]=dp2[0]=max1=max2=add=0;
	int n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i],lsh[i]=a[i];
	sort(lsh+1,lsh+n+1); int m=unique(lsh+1,lsh+n+1)-lsh-1;
	for(int i=1;i<=n;i++) a[i]=lower_bound(lsh+1,lsh+m+1,a[i])-lsh;
	for(int i=2;i<=n;i++){
		int cg1=max(max1+add,dp1[a[i]]+lsh[a[i]]+add);
		int cg2=max(max2+add,dp2[a[i]]+lsh[a[i]]+add);
		if(a[i]==a[i-1]) add+=lsh[a[i]];
		dp1[a[i-1]]=max(dp1[a[i-1]],cg2-add),max1=max(max1,dp1[a[i-1]]);
		dp2[a[i-1]]=max(dp2[a[i-1]],cg1-add),max2=max(max2,dp2[a[i-1]]);
	}
	cout<<max(max1+add,max2+add)<<"\n";
}
signed main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int t; cin>>t;
	while(t--) solve();
	return 0;
}
/*
Notices
1. Multitest: print endl before return
2. Multitest: clear (test random ordered sample)
3. Memory
4. long long / int range
5. corner case (minimum and maximum)
6. non-void return value
7. beizeng shangjie
8. -Wall -Wextra -fsanitize=undefined when testing samples, -O2 when testing speed (do not write O2 in the code)
*/